import React, {Component} from 'react';
import {View, AsyncStorage} from 'react-native';
import {Button, Toast} from '@ant-design/react-native';
import api from '../config/api';

class PageOne extends Component {
  static navigationOptions = {
    title: '子页面',
  };

  save = () => {
    const token = '';
    // JSON.stringify(object): JSON对象转换为字符串 用来存储
    AsyncStorage.setItem('token', JSON.stringify(token), error => {
      if (error) {
        alert('存储失败');
        return;
      }
      alert('存储成功');
    });
  };

  read = () => {
    AsyncStorage.getItem('token', (error, result) => {
      if (!error) {
        console.log(result);
      }
    });
  };

  // POST请求
  xhrPost = () => {
    const postData = {
      page: 1,
      pageSize: 10,
      type: '',
      read: '',
    };
    api.xhrPost('/message/list', postData).then(res => {
      console.log(res);
    });
  };

  // Get请求
  xhrGet = () => {
    const getData = {
      siteId: 26,
    };
    api.xhrGet('/device/getCamerasBySiteId', getData).then(res => {
      console.log(res);
    });
  };

  test = () => {
    Toast.success('Load success !!!', 1);
  };

  render() {
    return (
      <View>
        <Button
          onPress={() =>
            this.props.navigation.push('Details', {
              itemId: 86,
              otherParam: '我是从第一个页面过来的',
            })
          }>
          Go to 详情页
        </Button>
        <View style={{marginTop: 20}}>
          <Button onPress={this.xhrPost}>POST请求</Button>
        </View>
        <View style={{marginTop: 20}}>
          <Button onPress={this.xhrGet}>Get请求</Button>
        </View>
        <View style={{marginTop: 20}}>
          <Button onPress={this.save}>本地存数据</Button>
        </View>
        <View style={{marginTop: 20}}>
          <Button onPress={this.read}>本地取数据</Button>
        </View>
        <View style={{marginTop: 20}}>
          <Button onPress={this.test}>测试Toast</Button>
        </View>
      </View>
    );
  }
}

export default PageOne;
